{include="header"}

{if condition="$fsc->proveedor"}
<script type="text/javascript">
   function comprobar_url()
   {
      $("#panel_generales").hide();
      $("#panel_facturacion").hide();
      $("#panel_cuentas").hide();
      $("#panel_direcciones").hide();
      $("#div_subcuentas").hide();
      $("#chart_albaranes_month").hide();
      $("#b_generales").removeClass('active');
      $("#b_facturacion").removeClass('active');
      $("#b_direcciones").removeClass('active');
      $("#b_subcuentas").removeClass('active');
      $("#b_stats").removeClass('active');
      
      if(window.location.hash.substring(1) == 'facturacion')
      {
         $("#panel_facturacion").show();
         $("#panel_cuentas").show();
         $("#b_facturacion").addClass('active');
         document.f_proveedor.codserie.focus();
      }
      else if(window.location.hash.substring(1) == 'direcciones')
      {
         $("#panel_direcciones").show();
         $("#b_direcciones").addClass('active');
      }
      else if(window.location.hash.substring(1) == 'subcuentas')
      {
         $("#div_subcuentas").show();
         $("#b_subcuentas").addClass('active');
      }
      else if(window.location.hash.substring(1) == 'stats')
      {
         $("#chart_albaranes_month").show();
         $("#b_stats").addClass('active');
      }
      else
      {
         $("#panel_generales").show();
         $("#b_generales").addClass('active');
         document.f_proveedor.nombre.focus();
      }
   }
   $(document).ready(function() {
      comprobar_url();
      window.onpopstate = function(){
         comprobar_url();
      }
      $("#b_eliminar").click(function(event) {
         event.preventDefault();
         if( confirm("¿Realmente desea eliminar este proveedor?") )
            window.location.href = '{$fsc->ppage->url()}&delete={$fsc->proveedor->codproveedor}';
      });
      $("#b_nueva_cuenta").click(function(event) {
         event.preventDefault();
         $("#modal_nueva_cuenta").modal('show');
         document.f_nueva_cuenta.descripcion.focus();
      });
      $("#b_nueva_direccion").click(function(event) {
         event.preventDefault();
         $("#modal_nueva_direccion").modal('show');
         document.f_nueva_direccion.provincia.focus();
      });
   });
</script>

<div class="container-fluid" style="margin-top: 10px; margin-bottom: 10px;">
   <div class="row">
      <div class="col-sm-3">
         <a href="{$fsc->url()}" class="btn btn-sm btn-default" title="Recarga la página">
            <span class="glyphicon glyphicon-refresh"></span>
         </a>
         <a href="{$fsc->ppage->url()}" class="btn btn-sm btn-default">
            <span class="glyphicon glyphicon-arrow-left"></span> &nbsp; Proveedores
         </a>
      </div>
      <div class="col-sm-7 text-center">
         <h2 style="margin-top: 0px;">Editar proveedor {$fsc->proveedor->codproveedor}</h2>
      </div>
      <div class="col-sm-2 text-right">
         <a href="#" id="b_eliminar" class="btn btn-sm btn-danger">
            <span class="glyphicon glyphicon-trash"></span> &nbsp; Eliminar
         </a>
      </div>
   </div>
</div>

{if condition="isset($_GET['stats'])"}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
   // Load the Visualization API and the piechart package.
   google.load('visualization', '1.0', {'packages':['corechart']});
   
   // Set a callback to run when the Google Visualization API is loaded.
   google.setOnLoadCallback(drawChart);
   
   // Callback that creates and populates a data table,
   // instantiates the pie chart, passes in the data and
   // draws it.
   function drawChart()
   {
      // Create the data table.
      var data = new google.visualization.DataTable();
      data.addColumn('string', 'mes');
      data.addColumn('number', 'compras');
      data.addRows([
      {loop="$fsc->stats_from_prov()"}
         ['{$value['mes']}', {$value['compras']}],
      {/loop}
      ]);
      
      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.AreaChart(document.getElementById('chart_albaranes_month'));
      chart.draw(data);
   }
</script>
{/if}

<div class="container-fluid">
   <div class="row">
      <div class="col-lg-2 col-md-2 col-sm-3">
         <div class="list-group">
            <a id="b_generales" href="#" class="list-group-item active">
               <span class="glyphicon glyphicon-dashboard"></span> &nbsp; Datos generales
            </a>
            <a id="b_facturacion" href="#facturacion" class="list-group-item">
               <span class="glyphicon glyphicon-euro"></span> &nbsp; Facturación
            </a>
            <a id="b_direcciones" href="#direcciones" class="list-group-item">
               <span class="glyphicon glyphicon-road"></span> &nbsp; Direcciones
            </a>
            <a id="b_subcuentas" href="#subcuentas" class="list-group-item">
               <span class="glyphicon glyphicon-credit-card"></span> &nbsp; Subcuentas
            </a>
            {loop="$fsc->extensions"}
               {if condition="$value->type=='button'"}
               <a id="b_{$value->from}" href="index.php?page={$value->from}&codproveedor={$fsc->proveedor->codproveedor}" class="list-group-item">
                  <span class="glyphicon glyphicon-list"></span> &nbsp; {$value->text}
               </a>
               {/if}
            {/loop}
            <a id="b_stats" href="{$fsc->url()}&stats=TRUE#stats" class="list-group-item">
               <span class="glyphicon glyphicon-stats"></span> &nbsp; Estadísticas
            </a>
         </div>
      </div>
      
      <div class="col-lg-10 col-md-10 col-sm-9">
         <form name="f_proveedor" action="{$fsc->url()}" method="post" class="form">
            <input type="hidden" name="codproveedor" value="{$fsc->proveedor->codproveedor}"/>
            <div class="panel panel-primary" id="panel_generales">
               <div class="panel-heading">
                  <h3 class="panel-title">Datos generales</h3>
               </div>
               <div class="panel-body">
                  <div class="form-group col-lg-6 col-md-6 col-sm-6">
                     Nombre:
                     <input class="form-control" type="text" name="nombre" value="{$fsc->proveedor->nombre}" autocomplete="off"/>
                  </div>
                  <div class="form-group col-lg-6 col-md-6 col-sm-6">
                     Nombre comercial:
                     <input class="form-control" type="text" name="nombrecomercial" value="{$fsc->proveedor->nombrecomercial}" autocomplete="off"/>
                  </div>
                  <div class="form-group col-lg-4 col-md-4 col-sm-4">
                     {#FS_CIFNIF#}:
                     <input class="form-control" type="text" name="cifnif" value="{$fsc->proveedor->cifnif}" autocomplete="off"/>
                  </div>
                  <div class="form-group col-lg-4 col-md-4 col-sm-4">
                     Teléfono 1:
                     <input class="form-control" type="text" name="telefono1" value="{$fsc->proveedor->telefono1}" autocomplete="off"/>
                  </div>
                  <div class="form-group col-lg-4 col-md-4 col-sm-4">
                     Teléfono 2:
                     <input class="form-control" type="text" name="telefono2" value="{$fsc->proveedor->telefono2}" autocomplete="off"/>
                  </div>
                  <div class="form-group col-lg-4 col-md-4 col-sm-4">
                     Fax:
                     <input class="form-control" type="text" name="fax" value="{$fsc->proveedor->fax}" autocomplete="off"/>
                  </div>
                  <div class="form-group col-lg-4 col-md-4 col-sm-4">
                     Email:
                     <input class="form-control" type="text" name="email" value="{$fsc->proveedor->email}" maxlength="50" autocomplete="off"/>
                  </div>
                  <div class="form-group col-lg-4 col-md-4 col-sm-4">
                     Web:
                     <input class="form-control" type="text" name="web" value="{$fsc->proveedor->web}" autocomplete="off"/>
                  </div>
                  <div>
                     Observaciones:
                     <textarea class="form-control" name="observaciones" rows="3">{$fsc->proveedor->observaciones}</textarea>
                  </div>
               </div>
               <div class="panel-footer text-right">
                  <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                     <span class="glyphicon glyphicon-floppy-disk"></span>
                     &nbsp; Guardar
                  </button>
               </div>
            </div>
            
            <div class="panel panel-primary" id="panel_facturacion">
               <div class="panel-heading">
                  <h3 class="panel-title">Facturación</h3>
               </div>
               <div class="panel-body">
                  <div class="form-group col-lg-6 col-md-6 col-sm-6">
                     <a href="{$fsc->serie->url()}">Serie</a>:
                     <select class="form-control" name="codserie">
                     {loop="$fsc->serie->all()"}
                        {if condition="$value->codserie==$fsc->proveedor->codserie"}
                        <option value="{$value->codserie}" selected="selected">{$value->descripcion}</option>
                        {else}
                        <option value="{$value->codserie}">{$value->descripcion}</option>
                        {/if}
                     {/loop}
                     </select>
                  </div>
                  <div class="form-group col-lg-6 col-md-6 col-sm-6">
                     <a href="{$fsc->forma_pago->url()}">Forma de pago</a>:
                     <select class="form-control" name="codpago">
                     {loop="$fsc->forma_pago->all()"}
                        {if condition="$value->codpago==$fsc->proveedor->codpago"}
                        <option value="{$value->codpago}" selected="selected">{$value->descripcion}</option>
                        {else}
                        <option value="{$value->codpago}">{$value->descripcion}</option>
                        {/if}
                     {/loop}
                     </select>
                  </div>
                  <div class="form-group col-lg-6 col-md-6 col-sm-6">
                     <a href="{$fsc->divisa->url()}">Divisa</a>:
                     <select class="form-control" name="coddivisa">
                     {loop="$fsc->divisa->all()"}
                        {if condition="$value->coddivisa==$fsc->proveedor->coddivisa"}
                        <option value="{$value->coddivisa}" selected="selected">{$value->descripcion}</option>
                        {else}
                        <option value="{$value->coddivisa}">{$value->descripcion}</option>
                        {/if}
                     {/loop}
                     </select>
                  </div>
                  <div class="form-group col-lg-6 col-md-6 col-sm-6">
                     <a href="http://www.facturascripts.com/community/item/regimen-de-iva-desde-la-version-2014-4b-se-puede-seleccionar-el-regimen-de-784.html" target="_blank">Régimen IVA</a>:
                     <select class="form-control" name="regimeniva">
                     {loop="$fsc->proveedor->regimenes_iva()"}
                        {if condition="$value==$fsc->proveedor->regimeniva"}
                        <option value="{$value}" selected="selected">{$value}</option>
                        {else}
                        <option value="{$value}">{$value}</option>
                        {/if}
                     {/loop}
                     </select>
                  </div>
               </div>
               <div class="panel-footer text-right">
                  <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                     <span class="glyphicon glyphicon-floppy-disk"></span>
                     &nbsp; Guardar
                  </button>
               </div>
            </div>
         </form>
         
         <div class="panel-group" id="panel_cuentas">
            {loop="$fsc->cuenta_banco->all_from_proveedor($fsc->proveedor->codproveedor)"}
            <form action="{$fsc->url()}#facturacion" method="post" class="form">
               <input type="hidden" name="codcuenta" value="{$value->codcuenta}"/>
               <input type="hidden" name="codproveedor" value="{$value->codproveedor}"/>
               <div class="panel panel-info">
                  <div class="panel-heading">
                     <h3 class="panel-title">Cuenta bancaria #{$value->codcuenta}</h3>
                  </div>
                  <div class="panel-body">
                     <div class="form-group">
                        Descripción:
                        <input class="form-control" type="text" name="descripcion" value="{$value->descripcion}" placeholder="Cuenta principal" autocomplete="off"/>
                     </div>
                     <div class="form-group col-lg-6">
                        <a target="_blank" href="http://es.wikipedia.org/wiki/International_Bank_Account_Number">IBAN</a>:
                        <input class="form-control" type="text" name="iban" value="{$value->iban}" maxlength="28" placeholder="ES12345678901234567890123456" autocomplete="off"/>
                     </div>
                     <div class="form-group col-lg-6">
                        Calcular IBAN:
                        <input class="form-control" type="text" name="ciban" maxlength="20" placeholder="ENTIDAD SUCURSAL DC CUENTA" autocomplete="off"/>
                     </div>
                  </div>
                  <div class="panel-footer text-right">
                     <a class="btn btn-sm btn-danger pull-left" type="button" href="{$fsc->url()}&delete_cuenta={$value->codcuenta}#facturacion">
                         <span class="glyphicon glyphicon-trash"></span>
                         &nbsp; Eliminar
                     </a>
                     <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                         <span class="glyphicon glyphicon-floppy-disk"></span>
                         &nbsp; Guardar
                     </button>
                  </div>
               </div>
            </form>
            {/loop}
            <div class="panel panel-success">
               <div class="panel-heading">
                  <h3 class="panel-title">
                     <a id="b_nueva_cuenta" href="#">Nueva cuenta bancaria...</a>
                  </h3>
               </div>
            </div>
         </div>
         
         <div class="panel-group" id="panel_direcciones">
            {loop="$fsc->proveedor->get_direcciones()"}
            <form action="{$fsc->url()}#direcciones" method="post" class="form">
               <input type="hidden" name="codproveedor" value="{$fsc->proveedor->codproveedor}"/>
               <input type="hidden" name="coddir" value="{$value->id}"/>
               <div class="panel panel-info">
                  <div class="panel-heading">
                     <h3 class="panel-title">{$value->descripcion}</h3>
                  </div>
                  <div class="panel-body">
                     <div class="form-group col-lg-4 col-md-4 col-sm-4">
                        <a href="{$fsc->pais->url()}">País</a>
                        <select class="form-control" name="pais">
                        {loop="$fsc->pais->all()"}
                        <option value="{$value2->codpais}"{if condition="$value1->codpais==$value2->codpais"} selected="selected"{/if}>{$value2->nombre}</option>
                        {/loop}
                        </select>
                     </div>
                     <div class="form-group col-lg-4 col-md-4 col-sm-4">
                        <span class="text-capitalize">{#FS_PROVINCIA#}</span>:
                        <input class="form-control" type="text" name="provincia" value="{$value->provincia}"/>
                     </div>
                     <div class="form-group col-lg-4 col-md-4 col-sm-4">
                        Ciudad:
                        <input class="form-control" type="text" name="ciudad" value="{$value->ciudad}"/>
                     </div>
                     <div class="form-group col-lg-3 col-md-3 col-sm-3">
                        Código Postal:
                        <input class="form-control" type="text" name="codpostal" value="{$value->codpostal}" autocomplete="off"/>
                     </div>
                     <div class="form-group col-lg-9 col-md-9 col-sm-9">
                        Dirección:
                        <input class="form-control" type="text" name="direccion" value="{$value->direccion}" autocomplete="off"/>
                     </div>
                     <div class="form-group col-lg-4 col-md-4 col-sm-4">
                        {#FS_APARTADO#}:
                        <input class="form-control" type="text" name="apartado" value="{$value->apartado}" autocomplete="off"/>
                     </div>
                     <div class="radio col-lg-3 col-md-3 col-sm-3">
                        <label>
                           {if condition="$value->direccionppal"}
                           <input type="checkbox" name="direccionppal" id="direccionppal_{$value->id}" value="TRUE" checked="checked"/>
                           {else}
                           <input type="checkbox" name="direccionppal" id="direccionppal_{$value->id}" value="TRUE"/>
                           {/if}
                           Dirección principal
                        </label>
                     </div>
                     <div class="form-group col-lg-5 col-md-5 col-sm-5">
                        Descripción:
                        <input class="form-control" type="text" name="descripcion" value="{$value->descripcion}" autocomplete="off"/>
                     </div>
                  </div>
                  <div class="panel-footer text-right">
                     <a class="btn btn-sm btn-danger pull-left" type="button" href="{$fsc->url()}&delete_dir={$value->id}#direcciones">
                         <span class="glyphicon glyphicon-trash"></span>
                         &nbsp; Eliminar
                     </a>
                     <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                         <span class="glyphicon glyphicon-floppy-disk"></span>
                         &nbsp; Guardar
                     </button>
                  </div>
               </div>
            </form>
            {/loop}
            <div class="panel panel-success">
               <div class="panel-heading">
                  <h3 class="panel-title">
                     <a id="b_nueva_direccion" href="#">Nueva dirección...</a>
                  </h3>
               </div>
            </div>
         </div>
         
         <div class="table-responsive" id="div_subcuentas">
            <div class="table-responsive">
               <table class="table table-hover">
                  <thead>
                     <tr>
                        <th class="text-left">Ejercicio</th>
                        <th></th>
                        <th class="text-left">Subcuenta + Descripción</th>
                        <th class="text-right">Debe</th>
                        <th class="text-right">Haber</th>
                        <th class="text-right">Saldo</th>
                     </tr>
                  </thead>
                  {loop="$fsc->proveedor->get_subcuentas()"}
                  <tr>
                     <td><div class="form-control">{$value->codejercicio}</div></td>
                     <td class="text-right">
                        <a class="btn btn-sm btn-default" href="index.php?page=subcuenta_asociada&pro={$fsc->proveedor->codproveedor}&idsc={$value->idsubcuenta}">
                           <span class="glyphicon glyphicon-wrench"></span>
                        </a>
                     </td>
                     <td>
                        <div class="form-control">
                           <a href="{$value->url()}">{$value->codsubcuenta}</a> {$value->descripcion}
                        </div>
                     </td>
                     <td>
                        <div class="form-control text-right">{$fsc->show_precio($value->debe, $value->coddivisa)}</div>
                     </td>
                     <td>
                        <div class="form-control text-right">{$fsc->show_precio($value->haber, $value->coddivisa)}</div>
                     </td>
                     <td>
                        <div class="form-control text-right">{$fsc->show_precio($value->saldo, $value->coddivisa)}</div>
                     </td>
                  </tr>
                  {/loop}
                  <tr>
                     <td colspan="6" class="text-center">
                        <a class="btn btn-sm btn-block btn-success" href="index.php?page=subcuenta_asociada&pro={$fsc->proveedor->codproveedor}">Asignar una nueva subcuenta...</a>
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         
         <div id="chart_albaranes_month" style="height: 400px;"></div>
      </div>
   </div>
</div>

<form name="f_nueva_cuenta" action="{$fsc->url()}#facturacion" method="post" class="form">
   <input type="hidden" name="codproveedor" value="{$fsc->proveedor->codproveedor}"/>
   <div class="modal" id="modal_nueva_cuenta">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
               <h4 class="modal-title">Nueva cuenta bancaria</h4>
            </div>
            <div class="modal-body">
               <div class="form-group">
                  Descripción:
                  <input class="form-control" type="text" name="descripcion" placeholder="Cuenta principal" autocomplete="off"/>
               </div>
               <div class="form-group">
                  <a target="_blank" href="http://es.wikipedia.org/wiki/International_Bank_Account_Number">IBAN</a>:
                  <input class="form-control" type="text" name="iban" maxlength="28" placeholder="ES12345678901234567890123456" autocomplete="off"/>
               </div>
               <div class="form-group">
                  Calcular IBAN:
                  <input class="form-control" type="text" name="ciban" maxlength="20" placeholder="ENTIDAD SUCURSAL DC CUENTA" autocomplete="off"/>
               </div>
            </div>
            <div class="modal-footer">
               <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();" title="Guardar">
                  <span class="glyphicon glyphicon-floppy-disk"></span>
                  &nbsp; Guardar
                </button>
            </div>
         </div>
      </div>
   </div>
</form>

<form name="f_nueva_direccion" action="{$fsc->url()}#direcciones" method="post">
   <input type="hidden" name="codproveedor" value="{$fsc->proveedor->codproveedor}"/>
   <input type="hidden" name="coddir" value=""/>
   <div class="modal" id="modal_nueva_direccion">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
               <h4 class="modal-title">Nueva dirección</h4>
            </div>
            <div class="modal-body">
               <div class="form-group">
                  <a href="{$fsc->pais->url()}">País</a>:
                  <select class="form-control" name="pais">
                     {loop="$fsc->pais->all()"}
                     <option value="{$value->codpais}"{if condition="$value->is_default()"} selected="selected"{/if}>{$value->nombre}</option>
                     {/loop}
                  </select>
               </div>
               <div class="form-group">
                  {#FS_PROVINCIA#}:
                  <input class="form-control" type="text" name="provincia" value="{$fsc->empresa->provincia}"/>
               </div>
               <div class="form-group">
                  Ciudad:
                  <input class="form-control" type="text" name="ciudad" value="{$fsc->empresa->ciudad}"/>
               </div>
               <div class="form-group">
                  Código Postal:
                  <input class="form-control" type="text" name="codpostal" autocomplete="off"/>
               </div>
               <div class="form-group">
                  Dirección:
                  <input class="form-control" type="text" name="direccion" autocomplete="off"/>
               </div>
               <div class="form-group">
                  {#FS_APARTADO#}:
                  <input class="form-control" type="text" name="apartado" autocomplete="off"/>
               </div>
               <div class="checkbox">
                  <label>
                     <input type="checkbox" name="direccionppal" value="TRUE" checked="checked"/>
                     Dirección principal
                  </label>
               </div>
               <div class="form-group">
                  Descripción:
                  <input class="form-control" type="text" name="descripcion" value="Nueva dirección"/>
               </div>
            </div>
            <div class="modal-footer">
               <button class="btn btn-sm btn-primary" type="submit" onclick="this.disabled=true;this.form.submit();">
                  <span class="glyphicon glyphicon-floppy-disk"></span>
                  &nbsp; Guardar
               </button>
            </div>
         </div>
      </div>
   </div>
</form>
{else}
<div class="text-center">
   <img src="view/img/fuuu_face.png" alt="fuuuuu"/>
</div>
{/if}

{include="footer"}